#include "TypeWriter.h"

using namespace std;

TypeWriter::TypeWriter(int lettersPerSecond, string content, string fontPath, int size, int renderMethod) : SDLText("", fontPath, size, renderMethod)
	,mTextToWrite(content)
	,mIsWriting(false)
	,mNowLetterIndex(0)
	,mLettersPerSecond(lettersPerSecond)
	,mWriteTimer(0)
{
}

TypeWriter::~TypeWriter()
{
}

void TypeWriter::Update()
{
	//Spacebar writes everything down instantly
	//Right ALT triples the speed of typewriting
	if(mIsWriting && !SDLInput::GetInstance()->IsDown(SDLK_SPACE))
	{
		int speedDenum;
		if(SDLInput::GetInstance()->IsHeld(SDLK_RALT))
			speedDenum = mLettersPerSecond*3;
		else
			speedDenum = mLettersPerSecond;

		if(mWriteTimer >= (1.0f/speedDenum))
		{
			mWriteTimer = 0;
			mNowLetterIndex++;
			mContent = mTextToWrite.substr(0, mNowLetterIndex);
			if(mNowLetterIndex >= (mTextToWrite.length() - 1))
				mIsWriting = false;
			UpdateText();
		}
		mWriteTimer += SDLTime::GetInstance()->GetDeltaTime();
	}
	else
	{
		mContent = mTextToWrite;
		UpdateText();
	}
}
void TypeWriter::Reset()
{
	mContent = "";
	mNowLetterIndex = 0;
	mWriteTimer = 0;
	mIsWriting = true;
}
void TypeWriter::Start()
{
	Reset();
}